package model;
import java.awt.Color;
import java.util.List;
import java.util.Iterator;

/**
 * Class Car implements Actor
 * @author Arjan Frans, Alwin Rombout, Jelte Verbree, Brendan Kanters
 * @version 1.0
 */
public class Car implements Actor
{
	private Field field;
	private Location location;
	private boolean alive;
	private static Color color = Color.cyan;
	//+1 for south; -1 for north
	private int direction;
	
	/**
	 * Constructor to create a new car
	 * @param field
	 * @param location
	 */
    public Car(Field field, Location location)
    {
        this.field = field;
        this.alive = true;
        setLocation(location);
        setDirection(+1);
    }
    
    /**
     * Method to set the location of a Car
     * @param newLocation
     */
    public void setLocation(Location newLocation)
    {
    	if(location != null) {
            field.clear(location);
        }
        location = newLocation;
        field.place(this, newLocation);
    }
    
    /**
     * Method to set the direction of a Car
     * @param direction
     */
    public void setDirection(int direction)
    {
    	this.direction = direction;
    }

    /**
     * Makes the car act
     * @param newActor
     */
    public void act(List<Actor> newActor)
    {
    	if(isAlive())
    	{
	        Location location = getLocation();  
	        drive(location);
    	}
    }
    
    /**
     * Make the car drive
     * @param location
     */
    public void drive(Location location)
    {
    	int row = location.getRow();
    	int col = location.getCol();
    	if(row+direction < 0 || row+direction >= field.getDepth())
    	{
    		if(direction == +1) row = 0;
    		if(direction == -1) row = field.getDepth()-1;
    	}
    	else {
    		row += direction;
    	}
    	if(field.getObjectAt(row,col) instanceof Animal)
    	{
    		((Animal) field.getObjectAt(row,col)).setDead();
    	}
    	Location newLocation = new Location(row,col);
    	if(field.getObjectAt(row,col) instanceof Hunter)
    	{
    		//do not move;
    		newLocation = location;
    	}
    	setLocation(newLocation);
    }
    
    /**
     * Get the location of a car
     * @return location
     */
	public Location getLocation() {
		return location;
	}
	
	/**
	 * get the location of the field
	 * @return field
	 */
	public Field getField() {
		return field;
	}

	/**
	 * check if the car is 'alive'
	 * @return alive
	 */
	public boolean isAlive()
    {
        return alive;
    }

	/**
	 * method to set a car to dead
	 */
	public void setDead()
    {
        alive = false;
        if(location != null) {
            field.clear(location);
            location = null;
            field = null;
        }
    }

	@Override
	/**
	 * overrides the getColor method
	 */
	public Color getColor() {
		return color ;
	}
}

